package db

import (
	"sync"

	_ "github.com/glebarez/sqlite"
	"xorm.io/xorm"
)

// 单例模式
var (
	instance *xorm.Engine
	once     sync.Once
)

// GetDB 获取数据库连接实例（单例模式）
func GetDB() *xorm.Engine {
	once.Do(func() {
		var err error
		instance, err = xorm.NewEngine("sqlite", "./exam.db")
		if err != nil {
			panic("Failed to connect to database: " + err.Error())
		}
		// 同步表结构
		err = instance.Sync2(new(Exam))
		if err != nil {
			panic("Failed to sync database: " + err.Error())
		}
	})
	return instance
}
